<template>
    <div class="Register" v-loading="loading">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item prop="UserName">
                <el-input placeholder="请输入用户名(昵称)" v-model.trim="ruleForm.UserName" autocomplete="off">
                    <i slot="prefix" class="el-input__icon el-icon-user"></i>
                </el-input>
            </el-form-item>
            <el-form-item prop="Email">
                <el-input placeholder="请输入账号(邮箱号)" v-model="ruleForm.Email" autocomplete="off">
                    <i slot="prefix" class="el-input__icon el-icon-message"></i>
                </el-input>
            </el-form-item>
            <el-form-item prop="pwd">
                <el-input type="password" placeholder="请输入密码" show-password v-model.trim="ruleForm.pwd" autocomplete="off">
                    <i slot="prefix" class="el-input__icon el-icon-lock"></i>
                </el-input>
            </el-form-item>
            <el-form-item prop="CheckPwd">
                <el-input type="password" placeholder="请确认密码" show-password v-model.trim="ruleForm.CheckPwd" autocomplete="off">
                    <i slot="prefix" class="el-input__icon el-icon-lock"></i>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style="width: 100%;" @click="submitForm('ruleForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name: "Register",
    data() {
        var checkUserName = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('用户名不能为空'));
            }else{
                callback()
            }
        };
        var checkEmail = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('账号不能为空'));
            }
            let reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
            if(!reg.test(value)){
                return callback(new Error('账号格式有误！'));
            }else{
                callback()
            }
        };
        var checkPwd = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('密码不能为空'));
            }
            let reg = /^\w{5,7}$/
            if(!reg.test(value)){
                return callback(new Error('密码格式有误，请输入6-8位数字字母组合！'));
            }else{
                callback()
            }
        };
        var checkPassPwd= (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pwd) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                UserName:"",
                Email:"",
                pwd:"",
                CheckPwd:""
            },
            rules: {
                UserName:[
                    { validator: checkUserName, trigger:  ['blur', 'change'] }
                ],
                Email: [
                    { validator: checkEmail, trigger:  ['blur', 'change'] }
                ],
                pwd: [
                    { validator: checkPwd, trigger:  ['blur', 'change'] }
                ],
                CheckPwd:[
                { validator: checkPassPwd, trigger:  ['blur', 'change'] }
                ]
            },
            loading:false
        }
    },
    methods: {
        submitForm(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    console.log(this.ruleForm);
                    this.loading = true
                    setTimeout(()=>{
                        this.loading = false
                    },1000)
                } else {
                    return false;
                }
            });
        }
    }
}
</script>
<style lang="less" scoped>
.Register {
    width: 90%;
    height: 100%;
    margin: 0 auto;

    .demo-ruleForm {
        padding-top: 30px;
    }
}

/deep/ .el-form-item__content {
    margin-left: 0 !important;
}
</style>